package com.jiyi.activity.datainput.jm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jiyi.activity.WebActivity;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.WebViewBundle;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/30.
 */

public class JMDataInputActivity extends ActivityBase {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.tvTip)
    TextView tvTip;

    String build_no;
    final int BASE = 1;
    //1 读  2 写
    int type = 2;
    //是否可以查看报表
    boolean canViewReport = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_input_jm);
        ButterKnife.bind(this);
        init();
    }

    /**
     * @desc
     * @author hym
     * @createDate 2018/1/17 15:48
     */
    private void init() {
        getIntentData();
        setTop();
    }

    private void getIntentData() {
        type = getIntent().getIntExtra("type",1);
        build_no = getIntent().getStringExtra("build_no");
        canViewReport = getIntent().getBooleanExtra("canViewReport", false);
    }

    private void setTop() {
        if (type == 1) {
            tvTitle.setText("项目查看");
            tvTip.setVisibility(View.GONE);
        }
        else {
            tvTitle.setText("项目新增");
        }

        if (canViewReport) {
            tvSearch.setCompoundDrawables(null, null, null, null);
            tvSearch.setText("查看报表");
            tvSearch.setVisibility(View.VISIBLE);
        }
        else {
            tvSearch.setVisibility(View.GONE);
        }
    }

    @OnClick({ R.id.cvBase, R.id.cvOut, R.id.cvSave, R.id.cvEquip, R.id.cvJin,
            R.id.cvEscape, R.id.cvDianQi, R.id.cvBuild, R.id.cvPower,
            R.id.tvBack,R.id.tvSearch })
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.cvBase:
                // 建筑基础属性

                if (type == 1) {
                    intent = new Intent(this, DataInputBaseReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputBaseActivity.class);
                }

                intent.putExtra("build_no", build_no);
                startActivityForResult(intent, BASE);
                break;
            case R.id.cvOut:
                if (!isBase()) {
                    return;
                }
                // 外保温材料
                if (type == 1) {
                    intent = new Intent(this, DataInputWBWReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputWBWActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvSave:
                if (!isBase()) {
                    return;
                }
                // 救援场地
                if (type == 1) {
                    intent = new Intent(this, DataInputJYCDReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputJYCDActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvEquip:
                if (!isBase()) {
                    return;
                }
                // 建筑消防设施
                if (type == 1) {
                    intent = new Intent(this, DataInputXFSSReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputXFSSActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvJin:
                if (!isBase()) {
                    return;
                }
                // 管井防火封堵
                if (type == 1) {
                    intent = new Intent(this, DataInputGJFHFDReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputGJFHFDActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvEscape:
                if (!isBase()) {
                    return;
                }
                // 安全疏散
                if (type == 1) {
                    intent = new Intent(this, DataInputAQSSReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputAQSSActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvDianQi:
                if (!isBase()) {
                    return;
                }
                // 电气燃气安全
                if (type == 1) {
                    intent = new Intent(this, DataInputDQRQAQReadActiviy.class);
                }
                else {
                    intent = new Intent(this, DataInputDQRQAQActiviy.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvBuild:
                if (!isBase()) {
                    return;
                }
                // 老旧高层住宅建筑
                if (type == 1) {
                    intent = new Intent(this, DataInputLJZZJZReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputLJZZJZActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.cvPower:
                if (!isBase()) {
                    return;
                }
                // 消防力量建设
                if (type == 1) {
                    intent = new Intent(this, DataInputXFLLJSReadActivity.class);
                }
                else {
                    intent = new Intent(this, DataInputXFLLJSActivity.class);
                }
                intent.putExtra("build_no", build_no);
                startActivity(intent);
                break;
            case R.id.tvSearch:
                intent = new Intent(this,WebActivity.class);
                intent.putExtra("title", "报表");
                intent.putExtra("rightClickTitle", "下载");
                intent.putExtra("WebRightClickCallBack", WebActivity.RightClickEvent.downLoadDataInput);
                intent.putExtra("isHideTitle", false);
                WebViewBundle mWebViewBundle = new WebViewBundle();
                mWebViewBundle.addKeyMap("access_token", UserInfoUtil.getCurrentUserAccessToken());
                mWebViewBundle.addKeyMap("build_info_id", build_no); //http://m.jyfire.cn/Api/FireSafety/export
                mWebViewBundle.addKeyMap("postUrl", AppConfig.TEXT_HOST+"/Api/FireSafety/export");
                Bundle bundle = mWebViewBundle.build();
                intent.putExtras(bundle);

                intent.putExtra("url", WebApi.DATA_INPUT_REPORT+"/access_token/"+ UserInfoUtil.getCurrentUserAccessToken()+"/build_info_id/"+build_no);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == BASE) {
            if (resultCode == RESULT_OK) {
                build_no = data.getStringExtra("build_no");
            }
        }
    }

    public boolean isBase() {
        if (TextUtils.isEmpty(build_no) && type == 2) {
            ToastUtil.getInstances().showMsg("请先完成基础信息");
            return false;
        }
        return true;
    }
}
